
<!DOCTYPE html>
<html>
    <head>
        <title>Export Import</title>
        <meta charset="UTF-8">                
        <script src="../../../../lib/core/ht.js"></script> 
        <script src="../../../../lib/plugin/ht-form.js"></script> 
        <script src="../../../../lib/plugin/ht-obj.js"></script> 
        
        <script>
            function getRawText(obj){
                var text = String(obj); 
                return text.substring(14, text.length-3);
            }            
        </script> 
        
        <script src="obj/meter_obj.js"></script> 
        <script src="obj/meter_mtl.js"></script>                         
        
        <script>
            function init(){                             
                ht.Default.setImage('meter', 256, 256, '');                
                
                dataModel = new ht.DataModel();
                
                g3d = new ht.graph3d.Graph3dView(dataModel);
                propertyView = new ht.widget.PropertyView(dataModel);
                formPane = new ht.widget.FormPane();
                rightSplit = new ht.widget.SplitView(propertyView, formPane, 'v', 100);
                        
                new ht.widget.SplitView(g3d, rightSplit, 'h', 0.65).addToDOM();                         
                
                var params = {center: true};
                var modelMap = ht.Default.parseObj(meter_obj, meter_mtl, params);
                var array = [];
                for(var name in modelMap){
                    var model = modelMap[name];
                    array.push(model);
                    
                    if(name === 'pointer'){
                        model.mat = {
                            func: function(data){
                                var start = Math.PI * 0.736,
                                    range = Math.PI * 1.49,   
                                    angle = start - range * data.a('meter.value') / 4;
                                return ht.Default.createMatrix([
                                    { t3: [0, -82.5, 0] },
                                    { r3: [0, 0, angle] },
                                    { t3: [0, 82.5, 0]  }
                                ]);
                            }
                        };                         
                    }
                    if(name === 'switch'){
                        model.mat = {
                            func: function(data){
                                return ht.Default.createMatrix([
                                    { t3: [0, 48.5, 0] },
                                    { r3: [0, 0, data.a('meter.angle')] },
                                    { t3: [0, -48.5, 0]  }
                                ]);
                            }
                        }; 
                        model.color = {
                            func: function(data){
                                if(data.a('meter.angle')){
                                    return 'rgb(186, 0, 0)';
                                }else{
                                    return 'black';
                                }
                            }
                        };
                    }
                }
                ht.Default.setShape3dModel('meter', array);

                for(var i=0; i<3; i++){
                    var node = new ht.Node();
                    node.setTag(i);
                    node.setName('Meter - 00' + (i+1));
                    node.s({
                        'label.color': 'white',
                        'label.background': '#5271B8',
                        'label.face': 'center',
                        'label.position': 23,
                        'label.scale': 2,
                        'label.reverse.flip': true, 
                        
                        'note.scale': 1.5,
                        'note.t3': [-30, -5, -90], 
                        
                        'note2.scale': 1.2,
                        'note2.position': 17,
                        'note2.t3': [0, -20, -30],
                        'note2.color': 'black',
                        'note2.background': 'yellow', 
                        
                        'shape3d': 'meter',
                        'shape3d.scaleable': false,
                        'wf.visible': 'selected',
                        'select.brightness': 1
                    });
                    node.a({
                        'meter.value': i+1,
                        'meter.angle': i * Math.PI / 3
                    });
                    node.p3(i*200-200, params.rawS3[1]/2, i===1?100:-100);                    
                    node.r3(0, -Math.PI/6*(i-1), 0);
                    node.s3(params.rawS3);
                    dataModel.add(node);                   
                }
                dataModel.sm().ss(dataModel.getDataByTag(1));
                
                g3d.setGridVisible(true);
                g3d.getNote = function(data){
                    return 'Value:' + data.a('meter.value').toFixed(2);
                };
                g3d.getNote2 = function(data){
                    var value = Math.round(data.a('meter.angle') / Math.PI * 180);                    
                    return value ? 'Angle:' + value : 'Switch is off';
                };

                var oldEye = g3d.getEye().slice(0),
                    oldCenter = g3d.getCenter().slice(0),
                    newEye = [200, 300, 650],
                    newCenter = [0, params.rawS3[1]/2, 0];

                ht.Default.startAnim({                    
                    duration: 1000,
                    easing: function(t){ 
                        return (t *= 2) < 1 ? 0.5 * t * t : 0.5 * (1 - (--t) * (t - 2));                      
                    },
                    action: function(k){
                        g3d.setEye(
                            oldEye[0] + (newEye[0] - oldEye[0]) * k,
                            oldEye[1] + (newEye[1] - oldEye[1]) * k,
                            oldEye[2] + (newEye[2] - oldEye[2]) * k
                        );
                        g3d.setCenter(
                            oldCenter[0] + (newCenter[0] - oldCenter[0]) * k,
                            oldCenter[1] + (newCenter[1] - oldCenter[1]) * k,
                            oldCenter[2] + (newCenter[2] - oldCenter[2]) * k
                        );    
                    }                  
                });

                propertyView.addProperties([
                    {
                        name: 'name',
                        editable: true
                    },
                    {
                        name: 'meter.value',
                        accessType: 'attr',
                        editable: true,
                        slider: {
                            min: 0,
                            max: 4
                        }
                    },
                    {
                        name: 'meter.angle',
                        accessType: 'attr',
                        editable: true,
                        formatValue: function(value){
                            return Math.round(value / Math.PI * 180);
                        },
                        slider: {
                            min: 0,
                            max: Math.PI,
                            step: Math.PI/180*5,
                            getToolTip: function(){
                                return Math.round(this.getValue() / Math.PI * 180);
                            }
                        }
                    },
                    {
                        name: 'rotation',
                        editable: true,
                        formatValue: function(value){
                            return Math.round(value / Math.PI * 180);
                        },
                        slider: {
                            min: -Math.PI,
                            max: Math.PI,
                            step: Math.PI/180*5,
                            getToolTip: function(){
                                return Math.round(this.getValue() / Math.PI * 180);
                            }
                        }
                    }                    
                ]);               
            
                formPane.addRow([
                    {
                        id: 'export',
                        button: {                              
                            label: 'Export JSON',
                            onClicked: function(){
                                var json = dataModel.serialize();
                                formPane.v('textArea', json);
                            }
                        }
                    },
                    {
                        button: {                            
                            label: 'Import JSON',
                            onClicked: function(){
                                dataModel.clear();
                                dataModel.deserialize(formPane.v('textArea'));
                            }
                        }
                    }
                ],
                [0.1, 0.1]);                                  
                formPane.addRow([
                    {
                        id: 'textArea',
                        textArea: {
                        }
                    }
                ],
                [0.1], 0.1);  
                formPane.getItemById('export').element.onClicked();
            }  
            

        </script>
    </head>
    <body onload="init();">
    </body>
</html>
